package com.gack.business.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.StoreSupportFacityIconDao;
import com.gack.business.model.StoreSupportFacityIcon;
import com.gack.business.repository.StoreSupportFacityIconRepository;

/**
 * 
* @ClassName: StoreSupportFacityIconService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年9月7日 上午9:03:34 
* @version V1.0
 */
@Service
public class StoreSupportFacityIconService implements StoreSupportFacityIconServiceInterface{

	@Autowired
	private StoreSupportFacityIconRepository iconRepository;
	@Autowired
	private StoreSupportFacityIconDao iconDao;
	@Autowired
	private BackLogService backLog;
	
	private static final boolean IS_VALIDATE_NAME_EXISTS = true; // 是否需要验证名称是否存在
	
	@Override
	public Map<String, Object> edit(String backId, String id, String name, String icon) {
		Map<String, Object> map = new HashMap<>();
		List<String> nameList = new ArrayList<>(Arrays.asList(name.split("@;-;@")));
		List<String> iconList = new ArrayList<>(Arrays.asList(icon.split("@;-;@")));
		if(name == null || name.equals("")) {
			map.put("key", "error");
			map.put("msg", "icon名称不可为空");
			return map;
		}
		if(icon == null || icon.equals("")) {
			map.put("key", "error");
			map.put("msg", "icon不可为空");
			return map;
		}
		if(IS_VALIDATE_NAME_EXISTS && iconDao.hasName(id, name)) {
			map.put("key", "error");
			map.put("msg", "icon名称已存在");
			return map;
		}
		String logStr = null;
		List<String> existsFacityIconNames = iconDao.existsFacityIconNames(nameList);
		List<StoreSupportFacityIcon> facityIcons = new ArrayList<>();
		if(id == null || id.equals("")){
			Date now = new Date();
			int length = nameList.size() <= iconList.size() ? nameList.size() : iconList.size();
			for(int i = 0; i < length; i++) {
				if(existsFacityIconNames.contains(nameList.get(i))){
					continue;
				}
				StoreSupportFacityIcon facityIcon = new StoreSupportFacityIcon();
				facityIcon.setIs_delete(0);
				facityIcon.setName(nameList.get(i));
				facityIcon.setIcon(iconList.get(i));
				facityIcon.setUpdate_time(now);
				facityIcons.add(facityIcon);
			}
			logStr = "添加门店配套设施icon成功>> 名称："+nameList.toString()+", icon: "+iconList.toString();
		}else{
			StoreSupportFacityIcon facityIcon = iconRepository.findOne(id);
			if(facityIcon == null){
				map.put("key", "error");
				map.put("msg", "icon不存在");
				return map;
			}
			logStr = "编辑门店配套设施icon成功>> 名称："+name+", icon: "+icon;
		}
		facityIcons = iconRepository.save(facityIcons);
		if(facityIcons == null || facityIcons.size() == 0){
			map.put("key", "error");
			map.put("msg", "操作失败");
			return map;
		}
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}

	@Override
	public Map<String, Object> delete(String backId, String ids) {
		Map<String, Object> map = new HashMap<>();
		if(ids == null || ids.equals("")){
			map.put("key", "error");
			map.put("msg", "id不可为空");
			return map;
		}
		// 获取id列表
		List<String> idList = new ArrayList<>(Arrays.asList(StringUtils.split(ids,"@;-;@")));
		if(idList != null && idList.size() == 1 && iconDao.hasUsing(idList.get(0))) {
			map.put("key", "error");
			map.put("msg", "icon使用中");
			return map;
		}
		// 删除icon
		iconDao.delete(idList);
		String logStr = "删除门店配套设施icon成功>> ids:"+ids;
		backLog.addBackLog(backId, logStr);
		map.put("key", "success");
		map.put("msg", "操作成功");
		return map;
	}

	@Override
	public Map<String, Object> search(int searchType, String keyWord, String startTime, String endTime,
			int page, int size) {
		Map<String, Object> map = new HashMap<>();
		page = page < 0 ? 0 : page;
		size = size < 0 ? 1 : size; 
		List<StoreSupportFacityIcon> icons = iconDao.search(searchType, keyWord, startTime, endTime, page, size);
		long total = iconDao.searchTotal(searchType, keyWord, startTime, endTime);
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("icons", icons);
		map.put("total", total);
		return map;
	}

	@Override
	public Map<String, Object> allIcons() {
		Map<String, Object> map = new HashMap<>();
		List<StoreSupportFacityIcon> icons = iconDao.allIcons();
		int total = icons.size();
		map.put("key", "success");
		map.put("msg", "操作成功");
		map.put("icons", icons);
		map.put("total", total);
		return map;
	}

}
